'use strict';
let nunjucks=require('nunjucks');
function creatrEnv(path, opts) {
    path = path || 'views';
    opts = opts || {};
    let envOpts = {
        autoescape: opts.autoescape === undefined ? true : opts.autoescape,
        throwOnUndefined: opts.throwOnUndefined === undefined ? true : opts.throwOnUndefined,
        trimBlocks: opts.trimBlocks === undefined ? true : opts.trimBlocks,
        lstripBlocks: opts.lstripBlocks === undefined ? true : opts.lstripBlocks,
        watch: opts.watch === undefined ? true : opts.watch,
        noCache: opts.noCache === undefined ? true : opts.noCache
    }
    let env=nunjucks.configure(path,envOpts);
    return env;
};

module.exports= async (ctx,next)=>{
    ctx.render=(view,model,path,opts)=>{
        let env=creatrEnv(path,opts);
        ctx.body=env.render(view,model);
    }
    await next();
}